/* 
 * A664p7_support.c -- Implements general support functions.
 *
 */

// Disable deprecated warning for sprintf and scanf
//#pragma warning( disable : 4996 )

#include "A664p7_internal.h"
#include "stdio.h"
#include "string.h"
#include "log.h"

/* A664p7_auto_tx_mode: Generates TX mode flags to match available network
 *                    devices
 *  INPUTS:  net_a_dev -- A pointer to the network A dev, or NULL if Network A
 *                        is not used.
 *           net_b_dev -- A pointer to the network B dev, or NULL if Network B
 *                        is not used.
 *  RETURNS: An tx_mode value that can be used with create_vl
 */
int A664p7_auto_tx_mode()
{
	int mode = 0;
	mode |= VL_TX_A;
	return mode;
}

/* A664p7_auto_rx_mode: Generates RX mode flags to match available network
 *                    devices
 *  INPUTS:  net_a_dev -- A pointer to the network A dev, or NULL if Network A
 *                        is not used.
 *           net_b_dev -- A pointer to the network B dev, or NULL if Network B
 *                        is not used.
 *  RETURNS: An rx_mode value that can be used with create_vl
 */
int A664p7_auto_rx_mode()
{
	int mode = 0;
	mode |= VL_RX_A;
	return mode;
}

void mem_die(void *pointer)
{
	if(!pointer)
	{
		LOGE("Memory error: Unexpected NULL pointer detected! Exiting...\n");
	}
}


void show_mac_address(unsigned char addr[6])
{
	unsigned long i;
	unsigned short tmp;
	for(i = 0; i < sizeof(MAC_ADDRESS)-1; i++)
	{
		tmp = (unsigned short)addr[i];
		LOGD("%02hX:", tmp);
	}
	tmp = (unsigned short)addr[i];
	LOGD("%02hX", tmp);
}

void show_eth_header(PACKET *p)
{
	LOGD("DST: "); 
	show_mac_address(p->eh->dest);
	LOGD(" / SRC: ");
	show_mac_address(p->eh->source);
	LOGD("\n");
}

/* Convert an IP address to a string */
/* Parameters:
		in: unsigned long int containing the IP address to be converted
		out: pointer to a buffer long enough to hold an IP address (16 bytes) */
char *iptos(unsigned long in, char *out)
{
    unsigned char *p;
	p = (unsigned char *)&in;
	sprintf(out, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
	return out;
}
